const _ = require('lodash');
function shoppingCartService($http){

    // @Description 添加 商品到 购物车
    function addOrderItemToShoppingCart(food, shoppingCart, callback){
        
        console.log("sh>>>>"+shoppingCart);
        console.log(food._id);
        console.log(shoppingCart.length);
     
        let flag = true;
        if(shoppingCart.length != 0){
            _.forEach(shoppingCart, (item) => {
                if(item._id == food._id){
                    flag = false;
                    let newNum = item.num + food.num;
                    let newSubTotalMoney = food.price * newNum;
                    item.num = newNum;
                    item.subTotalMoney = newSubTotalMoney;
                }
            })
        }

        if(flag){
            food.subTotalMoney = food.num * food.price;
            shoppingCart.push(food);
        }
    


        callback(null, shoppingCart);
    }

    // @Description 点击购物车内部的 + 按钮
    function plusOneItem(shoppingCart, foodId, callback){
        if(foodId in shoppingCart){

        }else{
            console.log("当前shoppingCart 不存在商品Id "+foodId);

        }
        // _.forEach(shoppingCart, (item) => {
        //     item.
        // })
    }
    
    // @Description 点击购物车内部的  - 按钮
    function minusOneItem(){
        
    }

    // 从购物车中移除一条Item
    function removeOrderItemFromShoppingCart(){
        
    }


    return {
        addOrderItemToShoppingCart: addOrderItemToShoppingCart,
        plusOneItem: plusOneItem,
        minusOneItem: minusOneItem,
        removeOrderItemFromShoppingCart: removeOrderItemFromShoppingCart
    }
}

module.exports = shoppingCartService;